class Product < ActiveRecord::Base
  attr_accessible :category_id, :description, :item_number, :size_type
  # for simple form use
  attr_accessor :size_type
  attr_accessor :size_type_ids
  attr_accessible :size_type_ids

  # default_scope :order => 'item_number'

  belongs_to :category
  has_many :stocks
  has_many :sizes, :through => :stocks

  has_many :invoice_items
  has_many :purchase_items

  validates :item_number, :presence => true, :uniqueness => true
  # validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
  validates :category_id, :presence => true
  
  
  def self.search(search)
    if search
      where('item_number LIKE ?', "%#{search}%")
    else
      scoped
    end
  end
  
  def total_quantity
    quantity = 0
    
    self.stocks.each do |item|
      if item.quantity.is_a?(Integer) 
        quantity = quantity + item.quantity
      end  
    end  
    
    return quantity
  end

end
